/**
 * GeoSOA/Spatialytics/mapstyles/contexts/IntervalsThematicContext.js
 *
 * IntervalsThematicContext
 *
 * Copyright (C) 2008-2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090918
 **/

dojo.provide("GeoSOA.Spatialytics.mapstyles.contexts.IntervalsThematicContext");
dojo.require("GeoSOA.Spatialytics.mapstyles.contexts.BaseThematicContext");

/**
 * Context object for user-defined intervals.
 * Inherits from BaseThematicContext.
 * @param {Object} options Options passed to the constructor
 * 
 * @constructor
 */
dojo.declare("GeoSOA.Spatialytics.mapstyles.contexts.IntervalsThematicContext",
		GeoSOA.Spatialytics.mapstyles.contexts.BaseThematicContext, {
	
	constructor: function (options) {
	
		var options = options || {};
		
		// current context for closures
		var that = this;
		
		/**
		 * {Array} options.intervalsArray (optional) Array containing
		 * the minimum and maximum values for each interval class, along
		 * with the color associated with the interval. Each array element
		 * is an object of the form:
		 * { "min": 0, "max": 10, "color": "#00FF00" }
		 * where the "min" attribute is the lower boundary (inclusive),
		 * the "max" attribute is the upper boundary (exclusive) and
		 * the "color" attribute is the HTML color string ("#RRGGBB") associated
		 * to this interval class. With the above example, if we have
		 * 0 <= value < 10 (where value is the value attribute in the feature),
		 * the color returned by this.getColor will be pure green ("#00FF00").
		 * 
		 * Default value is an array with some intervals given as an example
		 * in the code; you should really specify your own intervals that are
		 * relevant for your map.
		 */
		this.intervalsArray = options.intervalsArray ||
		[
		 {"min":      0, "max":  100000, "color": "#003000"},
		 {"min": 100000, "max":  200000, "color": "#005000"},
		 {"min": 200000, "max":  300000, "color": "#007000"},
		 {"min": 300000, "max":  400000, "color": "#009000"},
		 {"min": 400000, "max":  500000, "color": "#00B000"},
		 {"min": 500000, "max": 3000000, "color": "#00D000"}
		 ];
	
	
		/**
		 * Closure function to lookup the feature's value against the intervals
		 * array and return a color according to the defined interval classes.
		 * 
		 * @param {OpenLayers.Feature} feature OpenLayers feature object to
		 * apply a style to (passed internally by OpenLayers)
		 * @return {String} HTML color code
		 * @public
		 */
		this.getColor = function (feature) {
			var val = feature.attributes[that.valueAttribute];
			var n = that.intervalsArray.length;
			for (var i = 0 ; i < n ; i++) {
				if ( val >= that.intervalsArray[i].min &&
						val < that.intervalsArray[i].max ) {
					return that.intervalsArray[i].color;
				}
			}
		};
	
	}

});
